package com.cm.log_process.log_common.util;

import lombok.Data;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author peter
 * @author : 邓志勇,黄鹏星
 * @title: TimestampLastDaysUtil
 * @projectName bison-web-service
 */
@Data
public class TimeUtils implements Serializable {


    //规范时间格式
    public static Long checkTime(String time) {
        if (time.endsWith("L")) {
            time = time.substring(0, time.length() - 1);
        }
        return Long.parseLong(time);
    }


    /**
     * 将时间戳转换成时间，精确到毫秒
     * @return
     */
    public static String timeChangeTimeStamp(Long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//这个是你要转成后的时间的格式
        return sdf.format(new Date(timeStamp));   // 时间戳转换成时间
    }
    /**
     * 获取当天时间，例如：2019:12:16:00:00:00:000
     * @return
     */
    public static Long getTodayTime(){
        SimpleDateFormat toDayTime = new SimpleDateFormat("yyyy-MM-dd 00:00:00:000");
        Calendar tt = Calendar.getInstance();
        Date data = null;
        try{
            data = toDayTime.parse(toDayTime.format(tt.getTime()));
        }catch (Exception e){
            e.printStackTrace();
        }
        if (data != null) {
            return data.getTime();
        }else {
            return null;
        }
    }
    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        return String.valueOf(ts);
    }


    //下一天凌晨
    public static long getTomorrowBegin(Long dateTime){
        //创建日历类
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(dateTime);
        c.add(Calendar.DATE,+1);
        c.set(Calendar.HOUR_OF_DAY,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND,0);
        //获取明天开始时间
        long TomorrowBegin = c.getTimeInMillis();
        return TomorrowBegin;
    }

    /**
     * 获取前一个小时的时间戳
     * @param date
     * @return
     */
    public static Long timestampBefore1Hour(String date){
        //将String转化为long类型的
        Long time = (Long.parseLong(date));
        return time - 1*60*60*1000;
    }

}
